package com.example.testdraw.charts;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.view.View;

/**
 * 带有初始绘制效果的饼图
 * 只是个demo
 * com.example.testdraw.charts.MoveChart
 * @author zhangyw
 */
public class MoveChart extends View {
	private Paint mPaints;
	private RectF mBigOval;
	private static final float SWEEP_INC = 2;
	private Float mSweep[] = null;
	float count = 0;
	float preSum = 0;
	int index = -1;
	List temp = new ArrayList<Float>();

	public MoveChart(Context context, Float rate[]) {
		super(context);
		// TODO Auto-generated constructor stub
		this.mSweep = rate.clone();
		for (int i = 0; i < mSweep.length; i++) {
			mSweep[i] *= 360;
		}
		mBigOval = new RectF(40, 10, 280, 250);
	}

	private void drawArcs(Canvas canvas, RectF oval, Float[] mSweeps) {
		mPaints = new Paint();
		mPaints.setAntiAlias(true);
		mPaints.setStyle(Paint.Style.FILL);
		float mStart = 0;
		for (int i = 0; i < mSweeps.length; i++) {
			switch (i % 3) {
			case 0:
				mPaints.setColor(Color.BLUE);
				break;
			case 1:
				mPaints.setColor(Color.RED);
				break;
			default:
				mPaints.setColor(Color.GREEN);
				break;
			}
			canvas.drawArc(oval, mStart, mSweeps[i], true, mPaints);
			mStart += mSweeps[i];
		}
	}

	@Override
	protected void onDraw(Canvas canvas) {
		count += SWEEP_INC;
		temp.add((count - preSum));
		drawArcs(canvas, mBigOval,
				(Float[]) temp.toArray(new Float[temp.size()]));
		temp.remove(index + 1);
		if ((count - preSum) >= mSweep[index + 1]) {
			index++;
			preSum += mSweep[index];
			temp.add(mSweep[index]);
		}
		if (count < 360) {
			this.invalidate();
		}
	}
	


}